package com.awspaas.user.apps.shhtaerospaceindustrial.guaranteecar;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge.weComSendAnnouncementUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;
/*
 * 加载个人申请业务保障用车信息
 */
@Controller
public class AppCmdApplyCarCancellation {
	/*
	 * 加载个人申请业务保障用车信息可取消订单
	 * 0:未提交|1:审批中|2:已审批|3:已取消
	 */
	@Mapping("com.awspaas.user.apps.loadingapplycarmessage")
	public static String getCateringReservation_saleDetails(UserContext uc) {
		String GUANLIYUAN =uc.getUserModel().getUserName();//登录人姓名
		JSONObject result = new JSONObject();
		try {
			Date now = new Date();// 系统日期
			SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String today = DateFormat.format(now);// 日期  
			String querySql = "select t.*  from  VIEW_CHECKPEOPLEAPPLYCAR  t  where t.BUSISTATUS in  ('1') "
			+ " and  t.YCKSDATE >=  '" + today + "' and t.TARGETMAN  ='" + GUANLIYUAN + "' order by  t.YCKSDATE  asc";
			List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			if (urlList == null || urlList.isEmpty()) {
				JSONArray itemArray = new JSONArray();
				result.put("status", "0");
				result.put("item", itemArray);
			}
			JSONArray itemArray = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject item = new JSONObject();
				String TARGETMAN = CoreUtil.objToStr(urlMap.get("TARGETMAN"));// 申请人
				String CARBEGINTIME = CoreUtil.objToStr(urlMap.get("CARBEGINTIME"));// 用车开始时间
				String CARENDTIME = CoreUtil.objToStr(urlMap.get("CARENDTIME"));// 用车结束时间
				String VEHICLEROUTE = CoreUtil.objToStr(urlMap.get("VEHICLEROUTE"));// 用车路线
				String BUSISTATUS = CoreUtil.objToStr(urlMap.get("BUSISTATUS"));// 状态
				String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
				item.put("targetman", TARGETMAN);
				item.put("carbegintime", CARBEGINTIME);
				item.put("carendtime", CARENDTIME);
				item.put("vehicleroute", VEHICLEROUTE);
				item.put("bindid", BINDID);
				item.put("busistatus", BUSISTATUS);
				itemArray.add(item);
			
			 }
			result.put("status", "0");
			result.put("item", itemArray);	

		} catch (Exception e) {

			result.put("status", "1");
			result.put("message", e.getMessage());
			e.printStackTrace();
		}

		return result.toString();
	}

	/*
	 * 用户取消已接单订单  
	 */
	@Mapping("com.awspaass.user.apps.cancellationapplycar")
	public String userCancelAssign(String bindid,String reason, UserContext uc) {
		JSONObject returnData = new JSONObject();
		if(null == bindid || "".equals(bindid)  ) {
			returnData.put("status", "0");
			returnData.put("message", "请检查参数!");
			return returnData.toString();
		}
		
		try {
			String querySql = "select *  from  VIEW_CHECKPEOPLEAPPLYCAR  WHERE BINDID ='" + bindid + "'";
			String BUSISTATUS = CoreUtil.objToStr(DBSql.getString(querySql, "BUSISTATUS"));// 状态
			String TARGETMAN = CoreUtil.objToStr(DBSql.getString(querySql, "TARGETMAN"));// 申请人
			String TARGETUNIT = CoreUtil.objToStr(DBSql.getString(querySql, "TARGETUNIT"));// 申请单位
			String WORKERID = CoreUtil.objToStr(DBSql.getString(querySql, "WORKERID"));// 申请人账号
			String TARGETMANPHONE = CoreUtil.objToStr(DBSql.getString(querySql, "TARGETMANPHONE"));// 联系电话
			String VEHICLEROUTE = CoreUtil.objToStr(DBSql.getString(querySql, "VEHICLEROUTE"));// 用车路线
			String CARBEGINTIME = CoreUtil.objToStr(DBSql.getString(querySql, "CARBEGINTIME"));// 用车开始时间
			String CARENDTIME = CoreUtil.objToStr(DBSql.getString(querySql, "CARENDTIME"));// 用车结束时间
			String DRIVER = CoreUtil.objToStr(DBSql.getString(querySql, "DRIVER"));// 派出驾驶员
			String JSYZH = CoreUtil.objToStr(DBSql.getString(querySql, "JSYZH"));// 驾驶员账号
			String DISPATCHERSIGN = CoreUtil.objToStr(DBSql.getString(querySql, "DISPATCHERSIGN"));// 调度员签字
			String DISPATCHERSIGNZH = CoreUtil.objToStr(DBSql.getString(querySql, "DISPATCHERSIGNZH"));// 调度员账号
			String LICENSENUM = CoreUtil.objToStr(DBSql.getString(querySql, "LICENSENUM"));// 派出车辆牌号
			String updateAssignSql = "update BO_EU_GUARANTEECAR set BUSISTATUS='3',CANCELREASON='"+reason+"'  where BINDID ='" + bindid + "'";
			DBSql.update(updateAssignSql);
			String text="【业务保障用车】"+TARGETMAN+"您好！您申请用车时间："+CARBEGINTIME+"--"+CARENDTIME+",用车路线："
					+ VEHICLEROUTE+"，已取消完成，谢谢！";
			String text1="【业务保障用车】"+DISPATCHERSIGN+"您好！申请人取消用车时间："+CARBEGINTIME+"--"+CARENDTIME+",用车路线："
					+ VEHICLEROUTE+",申请人："+TARGETMAN+"，联系电话："+TARGETMANPHONE+",如有问题请及时申请人，谢谢！";
			String text2="【业务保障用车】"+DRIVER+"您好！申请人取消用车时间："+CARBEGINTIME+"--"+CARENDTIME+",用车路线："
					+ VEHICLEROUTE+",申请人："+TARGETMAN+"，联系电话："+TARGETMANPHONE+",如有问题请及时申请人，谢谢！";
			if("1".equals(BUSISTATUS)) {
				try {
					System.out.println("准备终止流程！流程号:" + bindid + "用户ID:" + WORKERID);
					SDK.getProcessAPI().terminateById(bindid, WORKERID);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
			//释放车辆
			if(!"".equals(LICENSENUM)) {
				String updatechepaiSql = "update BO_EU_BZYC_CARDDOCINFO set INMISSION='否' where VEHICLECODE ='" + LICENSENUM + "'";
				DBSql.update(updatechepaiSql);
			}
			//发送企业微信消息
			if( !"".equals(WORKERID)  && !"".equals(TARGETUNIT)) {
				sendMessage(text,WORKERID,TARGETUNIT);
			}
			if( !"".equals(DISPATCHERSIGNZH)  && !"".equals(TARGETUNIT)) {
				sendMessage(text1,DISPATCHERSIGNZH,TARGETUNIT);
			}
			if( !"".equals(JSYZH)  && !"".equals(TARGETUNIT)) {
				sendMessage(text2,JSYZH,TARGETUNIT);
			}
			returnData.put("status", "0");
			returnData.put("message", "取消成功！");
		} catch (Exception e) {
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
			e.printStackTrace();
		}

		return returnData.toString();

	}
	public void sendMessage(String str,String peoplezh,String unit) {
		String sqluserId ="select EXT5  from ORGUSER  where USERID='"+peoplezh+"'";
        String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
        String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+unit+"'";
        List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
        if (jbData.size() > 0 && !"".equals(weixinid)) {
        	String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
			String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
			String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
			String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
			weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",str,0,agentid,acessToken);
        }
	}

}
